package cc.eddic.practice.concurrency;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.List;
import java.util.Map;

@Slf4j
@SpringBootApplication
public class ConcurrencyTyqApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ConcurrencyTyqApplication.class, args);
    }

    @Override
    public void run(String... args) {
        log.info("start");
        long start = System.currentTimeMillis();
        LogParserTyq0811Impl logParserTyq0811 = new LogParserTyq0811Impl();
        Map<String, List<LogItem>> map = logParserTyq0811.parse("D:\\SONY培训\\0811CompressOutPut");
        log.info("elapsed {} ms", System.currentTimeMillis() - start);
        map.forEach((key, value) -> System.out.println(key + ":" + value));
        if (log.isDebugEnabled()) log.debug("log文件总数" + map.entrySet().size());
        //老师讲课的多线程代码
//        long start = System.currentTimeMillis();
//        log.info("start");
////        loop();
////        executor();
//        log.info("elapsed {} ms", System.currentTimeMillis() - start);
//
//    private void executor(){
//        ExecutorService executorService= Executors.newFixedThreadPool(16);
//        CountDownLatch latch=new CountDownLatch(LOG_FILE_COUNT);
//        for (int i=0;i<LOG_FILE_COUNT;i++){
//            int finalI =i;
//            executorService.submit(()->dummyParse(finalI,latch));
//        }
//        try {
//            latch.await();
//        }catch (InterruptedException e){
//            log.error(e.getMessage());
//        }
//        ExecutorServiceUtil.shutdown(executorService);
//    }
//
//    private void dummyParse(int i,CountDownLatch latch){
//        dummyParse(i);
//        latch.countDown();
//    }
//
//    private void loop() {
//        for(int i=0;i<LOG_FILE_COUNT;i++){
//            dummyParse(i);
//        }
//    }
//
//    private void dummyParse(int i){
//        try {
//            val r=rnd(10,50);
//            if(log.isDebugEnabled()) log.debug("{} -> sleep {} ms",i,r);
//            Thread.sleep(r);
//        }catch (InterruptedException e){
//            log.error(e.getMessage());
//        }
//    }
//
//    private int rnd(int min,int max){
//        return random.nextInt(max-min+1)+min;
//    }
//
//    private final int LOG_FILE_COUNT=256;
//
//    private final Random random=new Random(System.currentTimeMillis());

//        //非多线程
//        long start = System.currentTimeMillis();
//        String path = "D:\\SONY培训\\nginx-logs\\nginx-logs";
//        LogParserTyqImpl logParser = new LogParserTyqImpl();
//        Map<String, List<LogItem>> map = logParser.parse(path);
//        if (log.isDebugEnabled()) log.debug("读取文件夹：" + map.keySet().size());
//        int num = 0;
//        int validNum = 0;
//        for (String s : map.keySet()) {
//            List<LogItem> list = map.get(s);
//            num += list.size();
//            validNum += list.stream().filter(LogItem::isValidRequest).count();
//        }
//        if (log.isDebugEnabled()) log.debug("总条数：" + num);
//        if (log.isDebugEnabled()) log.debug("有效条数：" + validNum);
//        long end = System.currentTimeMillis();
//        if (log.isDebugEnabled()) log.debug("：" + (end - start));
    }
}
